<?php


namespace app\index\model;


use think\Model;

class CustomerDiscount extends Model
{
    public static function getDiscountBYCustomerId($id): array
    {
        $CustomerDiscountClass=new self();
        $CustomerDiscountHistoryClass=new CustomerDiscountHistory();

        $data=$CustomerDiscountClass->where('customer_id','=',$id)->field('id,customer_id,balance')->find();
        if(empty($data)){
            $data=['id'=>0,'customer_id'=>$id,'balance'=>0];
            return ['status'=>true,'result'=>['data'=>$data,'history'=>[]]];
        }
        $data=$data->toArray();

        $history_data=$CustomerDiscountHistoryClass->alias('cdh')->join('order o','cdh.order_id=o.id','left')->where('cdh.customer_id',$id)->field('cdh.id,cdh.discount_id,o.order_id,cdh.customer_id,cdh.price,cdh.type,cdh.remark')->limit(100)->order('cdh.create_time desc')->select()->toArray();

        return ['status'=>true,'result'=>['data'=>$data,'history'=>$history_data]];
    }

}